var url = require('url');
var template = require('templateConfig');
var main = require('main');

//模板配置
var tplArr = [];
var moduleTpl =require('raw!../template/module.txt');
tplArr['module'] = template.compile(moduleTpl.replace(/^\s*|\s*$/g, ""));
var baseUrl= "/img/";

var goodsInfo = [{
    "id": 1,
    "title":"大家乐文胸一片式无痕内衣厚B杯深V聚拢商场同款内衣1",
    "attr": [],
    "num":"1002837828372",
    "spec":"大红,B杯,34/75",
    "quantity":"1",
    "inStock":"258",
    "colour":"红色、粉色、嫩肤",
    "cup":"A杯、B杯、C杯",
    "size":"32/70、40/75",
    "price":"99",
},{
    "id": 2,
    "title":"大家乐文胸一片式无痕内衣厚B杯深V聚拢商场同款内衣2",
    "attr": [],
    "num":"1002837828372",
    "spec":"大红,B杯,34/75",
    "quantity":"1",
    "inStock":"258",
    "colour":"红色、粉色、嫩肤",
    "cup":"A杯、B杯、C杯",
    "size":"32/70、40/75",
    "price":"98",
}];

var starInfo= {
    tType: "star",
    dataList: [{
        key: "评价",
        value: "4",  
        bigStar: true
    },{
        key: "商品包装",
        value: "0", 
        click: "starClick", 
    }]
};
//个人信息
var data_headgrid= {
    tType: "headgrid",
    image: "",
    title: "Golan Yao",
    tag: "超级粉丝",
    // content: "God"
};
//个人信息(竖列)
var data_headblock= {
    tType: "headblock",
    image: "",
    title: "Golan Yao",
    tag: "虚拟店员",
    content: [{
        key: "店员编号",
        value: "123"
    }],
    qrcode: "http://placeholder.qiniudn.com/300x300",
    qrlink: "",
    qrdetail: "扫码成为专享粉丝，享受粉丝福利！"
}; 
//积分
var data_account= {
    tType: "account",
    key: "当前积分",
    val: "1200",
    name: "去积分商城"
};
//超链接列表
var data_arrlist= {
    tType: "arrlist",
    dataList: [{
        name: "我的优惠券",
        link: ""
    },{
        name: "我收藏的商品",
        link: ""
    },{
        name: "我的收货地址",
        link: ""
    }]
};
//申请加入说明
var data_partner= {
    tType: "partner",
    title: "恭喜您！您已成为【超级粉丝】有机会成为【虚拟店员】",
    description: "虚拟店员可获得虚拟店员专享二维码，享有8折拿货。",
    link: "",
    name: "大家乐会员规则"
};            
//我的订单
var data_tabblock= {
    tType: "tabblock",
    title: "我的订单",
    baseUrl: baseUrl,
    dataList: [{
        image: "order_1.jpg",
        name: "全部订单",
        click: "",
        cornernum: "1"
    },{
        image: "order_2.jpg",
        name: "待付款",
        click: "",
        cornernum: "1"
    },{
        image: "order_3.jpg",
        name: "代收货",
        click: "",
        cornernum: "1"
    },{
        image: "order_4.jpg",
        name: "待评价",
        click: "",
        cornernum: "1"
    },{
        image: "order_5.jpg",
        name: "退换货",
        click: "",
        cornernum: ""
    }]
};  
//header
var data_header = {
    tType: "header",
    number: "No.62728"
}
var data_footer = {
    tType: "footer",
    active: "0",
    dataList: [{
        link: "",
        icon: "/img/f_icon2.jpg",
        name: "首页"
    },{
        link: "",
        icon: "/img/f_icon2.jpg",
        name: "购物车"
    },{
        link: "",
        icon: "/img/f_icon2.jpg",
        name: "个人中心"
    }]
}
function Module(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','mo-click'); 
        main.event(this,'change','mo-change'); 
        this.init();
        this.abc="123"
    }

    /**
     * [getModule 获取模板]
     * @param  {[json]} ret  [数据集]
     * @param  {[string]} name [模板名]
     * @return {[string]}      [生成html]
     */
    this.getModule= function(ret, name) {
        if(!name) return '';
        if(!ret){ret= {}}
        var data= ret;
        data.tType= name;
        return tplArr['module'](data);
    }

    //订单商品信息
    this.ordersGoodsInfo = function(ret){
        if(!ret){var ret = {}}
        var data = {
            "tType": "ordersGoodsInfo",
            "list": ret,
        }
        return tplArr['module'](data);
    }
    //省市区地址选择
    this.selectAddress = function(id,Fun){
        if(!id){var id = Math.ceil(Math.random()*1000)}
        if(!Fun){var Fun = ""}
        var data = {
            "tType": "selectaddress",
            "id": id,
            "Fun": Fun,
        }
        return tplArr['module'](data);
    }
    //获取省市区地址选择
    this.getSelectAddress = function(ths,type,val){
        var _this = this;
        var opt = "";
        var name = ths.attr("name");
        var sid = ths.attr("sid");
        if(type == 1){
            var val = ths.val();
            selectObj(name,val);
        }else{
            if(!val){val = ""}
            main.get(url.storeityLink,{"region_id":val},function(ret){
                var data = ret.data;
                for(var i=0;i<data.length;i++){
                    opt +="<option value="+data[i].region_id+">"+data[i].region_name+"</option>";
                }
                var html = ths.html(opt);
                val = html.val();
                if(val!==""){
                    if(name == "district"){ths.change();return false}
                    selectObj(name,val);
                }
            });
        }
        //下级递归
        function selectObj(name,val){
            var obj = "";
            name == "province"?obj = 'city':obj = 'district';
            _this.getSelectAddress($(".ui-form-item select[name='"+obj+"']"),0,val);
        }
    }
    //星级
    this.star= function(ret) {
        if(!ret) {ret= JSON.parse('{}');}
        var data= {
            tType: "star",
            dataList: ret
        }
            return tplArr['module'](data);
    }
    //星级点击
    this.starClick= function(ths, num) {
        var _c= ths.parents("div.stars");
        _c.find("i").removeClass("on");
        $.each(_c.find("i"),function(i,v){
            if(i+1<= num){
                $(this).addClass("on")
            }
        });

    }
    //输出内容
    this.outHtml = function(){
        $('.ui-container').html(
            _this.ordersGoodsInfo(goodsInfo)+
            _this.star(starInfo)+
            _this.selectAddress()
        );
    }
    //底部弹出层demo
    this.showBotActionsheet = function(){
        if(!ret){var ret = {}}
        var data = {
            "tType": "showBotActionsheet",
            "list": ret,
        }
        main.botActionsheet({"centent":tplArr['module'](data)});
    }
    // // 搜索框点击
    // this.Focus = function(){
    //     $('.ui-searchbar-wrap').addClass('focus');
    //     $('.ui-searchbar-input input').focus();
    // }
    // //搜索框取消
    // this.Cancel = function(){
    //     $('.ui-searchbar-wrap').removeClass('focus');
    // }

    // 搜索框js
    this.searchFocus = function(){
        $('.ui-searchbar-wrap').addClass('focus');
        $('.ui-searchbar-input input').focus();
    }
    //搜索框取消
    this.searchCancel = function(){
        $('.ui-searchbar-wrap').removeClass('focus');
    }
    // 选择属性
    this.Select= function(ths){
        ths.siblings('li').removeClass("current ui-tag-selected");
        ths.addClass("current ui-tag-selected");
        ths.siblings("input").val(ths.attr("lid"));
    } 
    this.init= function(){
        
        this.baseUrl= "/img/";
        //this.outHtml();
    	//main.alert();
    	//main.botActionsheet();
    }
    return this.main();
};

var mod = new Module();
module.exports = mod;